//
//  TGSignPreview.m
//  TongGuanLease
//
//  Created by 张奇 on 2022/6/30.
//

#import "TGSignPreview.h"
//#import "IMYWebView.h"

@interface TGSignPreview ()
//@property (nonatomic, strong) IMYWebView *webView;
@property (nonatomic,copy) NSString *linkStr;
@property (nonatomic,copy) FinishReloadBlockComplemention finishReloadBlock;

@property (nonatomic,assign) id<TGSignPreviewDelegate> delegate;
@end

@implementation TGSignPreview

- (instancetype)initWithLinkStr:(NSString *)linkStr
              finishReloadBlock:(FinishReloadBlockComplemention)finishReloadBlock {
    if (self = [super init] ) {
        self.backgroundColor = UIColorWhite;
        self.linkStr = linkStr;
        if (finishReloadBlock) {
            self.finishReloadBlock = finishReloadBlock;
        }
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithLinkStr:(NSString *)linkStr delegate:(id<TGSignPreviewDelegate>)delegate {
    if (self = [super init] ) {
        self.backgroundColor = UIColorWhite;
        self.linkStr = linkStr;
        self.delegate = delegate;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    
    _webView = [[IMYWebView alloc] init];
//    _webView.scrollView.scrollEnabled = NO;
    [_webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
    _webView.delegate = (id)self;
    [self addSubview:_webView];
    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    if (!EmptyCheck(self.linkStr)) {
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.linkStr]];
        [_webView loadRequest:request];
        [_webView reload];
    }
    
    
}

- (void)setLinkStr:(NSString *)linkStr {
    _linkStr = linkStr;
    if (!EmptyCheck(self.linkStr)) {
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.linkStr]];
        [_webView loadRequest:request];
        [_webView reload];
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if (self.webView.height != self.webView.scrollView.contentSize.height) {
        self.webView.height = self.webView.scrollView.contentSize.height;
        self.height = self.webView.scrollView.contentSize.height;
        if (self.finishReloadBlock) {
            self.finishReloadBlock(self.height);
        }
        if ([self.delegate respondsToSelector:@selector(signPreviewFinishLoadingWithHeight:)]) {
            [self.delegate signPreviewFinishLoadingWithHeight:self.height];
        }
        NSLog(@"---%.2f", self.webView.height);
        
        if (self.webView.scrollView.contentSize.width > SCREEN_WIDTH+10) {
            _webView.scrollView.scrollEnabled = YES;
        } else {
            _webView.scrollView.scrollEnabled = NO;
        }
    }
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.webView.scrollView removeObserver:self forKeyPath:@"contentSize"];
}

@end
